/*!
 * SimplyHtml (http://www.simplyhtml.com)
 * Copyright 2015 Franklin Schmidt, Hugo Teixeira
 * Licensed under MIT (http://opensource.org/licenses/MIT)
 */
var SimplyHtml = {};
+function() {
	'use strict';
	var utils = {
		clone: function(list) {
			var clone = [];
			for (var i = 0; i < list.length; i++)
				clone.push(list[i]);
			return clone;
		},
		setAttr: function(e, attr) {
			attr = attr || {};
			for (var k in attr) {
				e.setAttribute(k, attr[k]);
			}
		},
		newElement: function(tagName, attr) {
			var e = document.createElement(tagName);
			utils.setAttr(e, attr);
			return e;
		},
		transferChildren: function(newParent, oldParent) {
			var children = utils.clone(oldParent.childNodes);
			for (var i = 0; i < children.length; i++) {
				newParent.appendChild(children[i]);
			}
		},
		replaceWith: function(e, nodeName, attr) {
			var div = utils.newElement(nodeName, attr);
			utils.transferChildren(div, e);
			e.parentNode.replaceChild(div, e);
			utils.copyAttributes(div, e);
			return div;
		},
		moveChildrenAfter: function(e) {
			if (e.childNodes) {
				var children = utils.clone(e.childNodes);
				var len = children.length - 1;
				for (var i = len; i >= 0; i--) {
					e.parentNode.insertBefore(children[i], e.nextSibling);
				}
			}
		},
		htmlEncode: function(s) {
			s = s.replace(/</g,'&lt;');
			s = s.replace(/>/g,'&gt;');
			return s;
		},
		wrapWith: function(nodeName, e, attr) {
			var div = utils.newElement(nodeName, attr);
			e.parentNode.replaceChild(div, e);
			div.appendChild(e);
		},
		copyAttributes: function(newNode, oldNode) {
			var atts = oldNode.attributes;
			for (var i = 0; i < atts.length; i++) {
				if (atts[i].name == 'style') {
					var olds = oldNode.getAttribute('style') || '';
					var news = newNode.getAttribute('style') || '';
					var s = (olds+news).trim();
					s != '' && newNode.setAttribute('style', s);
				} else if (atts[i].name == 'class') {
                    $(newNode).addClass(atts[i].value);
				} else {
					newNode.setAttribute(atts[i].name, atts[i].value);
				}
			}
		},
		attrToStyle: function($e, attrName, cssProperty, fn) {
			var v = $e.attr(attrName);
			if (v != null) {
				v = fn? fn(v) : v;
				$e.css(cssProperty, v);
			}
		},
		attrToClass: function($e, attrName, className, fn) {
			className = className || attrName;
			var v = $e.attr(attrName);
			className = className.replace(/#/, v);
			v != null && $e.addClass(className) && fn && fn();
		},
		escape: function(value) {
			if (typeof value == 'string') {
				var hasSpace = value.indexOf(' ') >= 0;
				var hasQuote = value.indexOf('"') >= 0;
				value = value.replace(/\;/g, '%3B');
				value = value.replace(/"/g, '\\"');
				if (hasSpace || hasQuote)
					value = '"' + value + '"';
			}
			return value;
		},
		unescape: function(value) {
			if (value.charAt(0) == '"' && value.charAt(value.length-1) == '"')
				value = value.substring(1, value.length-1);

			value = value.replace(/\\"/g, '"');
			value = value.replace(/%3B/g, ';');
			return value;
		},
		setCookie: function(name, value) {
			var expires = new Date();
			expires.setFullYear(expires.getFullYear()+10);
			var curCookie = name + "=" + utils.escape(value) + ";expires=" + expires.toGMTString() + ";path=/";
			document.cookie = curCookie;
		},
		getCookie: function(name) {
			var dc = document.cookie;
			var prefix = name + "=";
			var begin = dc.indexOf("; " + prefix);
			if (begin == -1) {
				begin = dc.indexOf(prefix);
				if (begin != 0) return null;
			} else
				begin += 2;
			var end = document.cookie.indexOf(";", begin);
			if (end == -1)
				end = dc.length;
			return utils.unescape(dc.substring(begin + prefix.length, end));
		}
	};
	SimplyHtml.tags = {
		button: function(e) {
			var $e = $(e);
			$e.addClass('btn');
			$e.attr('large') != null && $e.addClass('btn-lg');
			$e.attr('small') != null && $e.addClass('btn-sm');
			$e.attr('extra-small') != null && $e.addClass('btn-xs');
			$e.hover(function() {
				$e.addClass('hover');
			}, function() {
				$e.removeClass('hover');
			});
		},
		div: function(e) {
			var $e = $(e);
			utils.attrToStyle($e, 'padding','padding', function(v) { return v.replace(/([0-9]+)([^\.\w]|$)/g, '$1px '); });
			utils.attrToClass($e, 'container');
			utils.attrToClass($e, 'row');
			utils.attrToClass($e, 'panel', 'panel panel-default');
			utils.attrToClass($e, 'colspan', 'col-xs-#');
		},
		form: function(e) {
			var $e = $(e);
			$e.attr('inline') != null && $e.addClass('form-inline');
			$e.attr('horizontal') != null && $e.addClass('form-horizontal') && $e.find('label').addClass('control-label');
		},
		hr: function(e) {
			var $e = $(e);
			utils.attrToClass($e, 'border');
			utils.attrToStyle($e, 'size','height', function(v) { return v.replace(/^([0-9]+)$/g, '$1px'); });
			utils.attrToStyle($e, 'color','background-color');
		},
		i: function(e) {
			var $e = $(e);
			var icon = $e.attr('glyphicon');
			if (icon) {
				utils.moveChildrenAfter(e);
				$e.addClass('fa fa-'+icon);
			}
		},
		iframe: function(e) {
			var $e = $(e);
			if ($e.attr('video') != null) {
				var src = e.innerHTML;
				if (src.match(/https?:\/\/www\.youtube\.com\/watch\?v=/)) {
					src = src.replace(/\/watch\?v=/,'/embed/');
					utils.setAttr(e,{width:425,height:350,src:src,frameborder:0,allowfullscreen:true});
				} else {
					var div = utils.replaceWith(e, 'DIV');
					div.innerHTML = utils.htmlEncode('<video>'+src+'</video>');
				}
			}
		},
		input: function(e) {
			var $e = $(e);
			var type = $e.attr('type');
			if (!type) {
				$e.attr('type','text');
			} else if (type == 'checkbox' || type == 'radio') {
				if (e.parentNode.nodeName.toLowerCase() == 'label')
					utils.wrapWith('DIV', e.parentNode, {class:type});
				return;
			} else if (type == 'submit') {
				SimplyHtml.tags.button(e);
				return;
			} else if (type.match(/file|radio/))
				return;
			$e.addClass('form-control');
			var s = $e.attr('size');
			s && $e.css({width:'auto',display:'inline'});
			if ($e.attr('autofocus'))
				$e.focus();
		},
		ol: function(e) {
			var $e = $(e);
			utils.attrToClass($e, 'breadcrumbs', 'breadcrumb');
		},
		select: function(e) {
			var $e = $(e);
			$e.addClass('form-control');
			var w = $e.attr('width');
			w && $e.css({width:w+'em',display:'inline'});
			if ($e.attr('autofocus'))
				$e.focus();
		},
		span: function(e) {
			var $e = $(e);
			utils.attrToClass($e, 'glyphicon', 'glyphicon glyphicon-#');
		},
		table: function(e) {
			var $e = $(e);
			$e.addClass('table');
			function replaceAttr(name, repl) {
				if ($e.attr(name) != null) {
					$e.addClass(repl);
				}
			};
			replaceAttr('border', 'table-bordered');
			replaceAttr('no-border', 'table-no-border');
			replaceAttr('striped', 'table-striped');
			replaceAttr('hover', 'table-hover');
			replaceAttr('condensed', 'table-condensed');
		},
		textarea: function(e) {
			var $e = $(e);
			$e.addClass('form-control');
			var c = $e.attr('cols');
			c && $e.css({width:'auto',display:'inline'});
			if ($e.attr('autofocus'))
				$e.focus();
		},
		ul: function(e) {
			var $e = $(e);
			utils.attrToClass($e, 'dropdown', 'dropdown-menu', function() {
				$e.attr('role','menu');
				var link = utils.newElement('A',{href:'javascript:void(0)','data-toggle':'dropdown','aria-haspopup':'true',role:'button','aria-expanded':'false'});
				var children = utils.clone(e.childNodes);
				for (var i = 0; i < children.length; i++) {
					var c = children[i];
					if (c.nodeName.toLowerCase() == 'li') {
						var $c = $(c);
						$c.attr('role','presentation');
						if ($c.attr('border') != null)
							$c.addClass('divider');
					} else {
						link.appendChild(c);
					}
				}
				link.appendChild(utils.newElement('SPAN',{class:'caret'}));
				var div = utils.newElement('DIV',{class:'dropdown'});
				e.parentNode.replaceChild(div, e);
				div.appendChild(link);
				div.appendChild(e);
			});
		}
	};
	function handleAttributes() {
		var attr = {
			textcolor: function(e, v) {
				e.style.color = v;
			},
			bgcolor: function(e, v) {
				e.style.backgroundColor = v;
				var nn = e.nodeName.toLowerCase();
				if (nn == 'button' || (nn == 'input' && e.getAttribute('type') == 'submit')) {
					e.style.borderColor = v;
				}
			},
			textsize: function(e, v) {
				e.style.fontSize = v;
			},
			right: function(e, v) {
				e.style.float = 'right';
			},
			left: function(e, v) {
				e.style.float = 'left';
			},
			'margin-top': function(e, v) {
				e.style.marginTop = v;
			},
			'margin-bottom': function(e, v) {
				e.style.marginBottom = v;
			}
		};
		var allElements = document.getElementsByTagName('*');
		for (var i = 0, n = allElements.length; i < n; i++) {
			var e = allElements[i];
			for (var k in attr) {
				var v = e.getAttribute(k);
				if (v != null) {
					attr[k](e, v);
				}
			}
		}
	};
	function doTags() {
		for (var k in SimplyHtml.tags) {
			var fn = SimplyHtml.tags[k];
			var list = document.getElementsByTagName(k);
			for (var i = list.length-1; i >= 0; i--) {
				fn(list[i]);
			}
		}
	};
	function handleResponsive() {
		var metas = document.getElementsByTagName('meta');
		for (var i = 0; i < metas.length; i++) {
			if (metas[i].getAttribute('name') == 'sizes') {
				var content = metas[i].getAttribute('content');
				var w = $(window).width();
				var screen = w > 1200? 'large' : w > 991? 'medium' : w > 600? 'small' : 'tiny';
				var cookie = utils.getCookie('scr');
				if (cookie == null || cookie != screen) {
					utils.setCookie('scr', screen);
					if (content.indexOf(screen) == -1)
						location.reload(true);
				}
				return;
			}
		}
	};
	$(document).ready(function() {
		handleResponsive();
		$(window).resize(handleResponsive);

		handleAttributes();
		doTags();
	});
}();